package com.wxproject.demo.Controller;

/*
import	java.util.HashMap;
 *项目名: WXTEST
 *文件名: EmailController
 *创建者: ZQF
 *创建时间:17/01/2021 19:16
 *描述: TODO
 */

import com.wxproject.demo.Common.MyException.BusinessException;
import com.wxproject.demo.Common.lang.ResultMap;
import com.wxproject.demo.Service.LoginService;
import com.wxproject.demo.Utils.RedisUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import static com.wxproject.demo.Common.exception.ResultEnum.EMPTY_ACTIVE_CODE;

@RestController
@Controller
@Log4j2
public class EmailController {
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private LoginService loginService;
    @Autowired
    private ResultMap resultMap;

    @PostMapping("/activeAccount")
    public Object activeAccount(@RequestParam("active")String active){
        try {
            String o = RedisUtil.get(active).toString();
            loginService.activeAccount(o);
            RedisUtil.del(active);
            return resultMap.success().code(200).message("激活成功");
        }catch (Exception e){
            log.info("active已失效");
            throw new BusinessException(EMPTY_ACTIVE_CODE);
        }
    }

//    @PostMapping("SendEmail")
//    public Object sendEmail(@RequestParam("email") String email){
//        Integer hasEmail = loginService.checkEmail(email);
//        Map<Object,Object> res = new HashMap<>();
//        if (hasEmail!=null){
//            res.put("code",401);
//            res.put("message","邮箱已被注册");
//            return res;
//        }
//        String code = RandowcodeUtil.getCheckCode();
//        redisUtil.set("Registercode-"+code+"-"+email,code,600);
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom("管理员"+'<'+"a812257301@sina.com"+'>');
//        message.setTo(email);
//        message.setSubject("注册验证码");
//        message.setText(code);
//        javaMailSender.send(message);
//        res.put("code",200);
//        res.put("message","发送成功");
//        return res;
//    }
}
